package example;


import com.ctg.faas.runtime.Context;
import com.ctg.faas.runtime.FunctionInitializer;
import com.ctg.faas.runtime.PojoRequestHandler;
import com.ctg.faas.runtime.PreStopHandler;
import example.model.SimpleRequest;
import example.model.SimpleResponse;

import java.io.IOException;

public class PojoHandler implements PojoRequestHandler<SimpleRequest, SimpleResponse>, FunctionInitializer, PreStopHandler {

    @Override
    public SimpleResponse handleRequest(SimpleRequest request, Context context) {
        String message = "Hello, " + request.getFirstName() + " " + request.getLastName();
        System.out.println(message);
        context.getLogger().info("input "+request.getFirstName());
        return new SimpleResponse(message);
    }

    /**
     * 做一些实例初始化的工作，比如初始化
     * @param context The function compute initialize environment context object.
     * @throws IOException
     */
    @Override
    public void initialize(Context context) throws IOException {
        context.getLogger().info("init");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        context.getLogger().info("init ok");
    }

    /**
     * 实例关停
     * @param context The function compute preStop environment context object.
     * @throws IOException
     */
    @Override
    public void preStop(Context context) throws IOException {
        context.getLogger().info("stop");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        context.getLogger().info("stop ok");
    }
}
